Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'cluster', 'namespace', and 'user' options to delete command. Enhance delete to load provider components from cluster. #384

Merged
merged 1 commit into from
Jun 25, 2018

Conversation

spew
Copy link
Contributor

@spew spew commented Jun 22, 2018

What this PR does / why we need it:
This PR adds the 'cluster', 'namespace', and 'user' options to the clusterctl delete cluster command. These options match what is available in kubectl. Also, the delete cluster command is enhanced to load the provider components from cluster.

Release note:

NONE

@kubernetes/kube-deploy-reviewers

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 22, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: spew

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 22, 2018
@spew spew force-pushed the delete-cluster-params branch 2 times, most recently from eef7f3c to 03ca60d Compare June 22, 2018 18:43
@spew
Copy link
Contributor Author

spew commented Jun 22, 2018

/assign @roberthbailey @mkjelland @k4leung4

@spew spew force-pushed the delete-cluster-params branch 2 times, most recently from 84c2870 to 28a7872 Compare June 22, 2018 20:31
@@ -65,15 +67,16 @@ func (c *clusterClient) removeKubeconfigFile() error {
return os.Remove(c.kubeconfigFile)
}

func NewClusterClientFromFile(kubeconfigFile string) (*clusterClient, error) {
c, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfigFile)
func NewClusterClientFromDefaultSearchPath(kubeconfigFile string, overrides tcmd.ConfigOverrides) (*clusterClient, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one treats the first param as a string and the prior treats it as the actual config? That seems backwards to me, but maybe that's because the libraries you are calling need to do the file handling....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both versions of this function treat the kubeconfigFile argument as a string. With the addition of the ability to select a specific Context in the Kubeconfig search path, I chose to rename the method to more accurately reflect reality, namely, if an empty string is passed in for kubeconfigFile then when iclientcmd.NewClusterApiClientForDefaultSearchPath(...) is called it will search for all kubeconfigs in the default search path ($KUBECONFIG, ${HOME}/.kube/config, etc).

What do you think?

@@ -65,15 +67,16 @@ func (c *clusterClient) removeKubeconfigFile() error {
return os.Remove(c.kubeconfigFile)
}

func NewClusterClientFromFile(kubeconfigFile string) (*clusterClient, error) {
c, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfigFile)
func NewClusterClientFromDefaultSearchPath(kubeconfigFile string, overrides tcmd.ConfigOverrides) (*clusterClient, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the name of this fn; why not name it NewFromFile(...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my reply on the other comment about parameters and function name.

clientSet clientset.Interface
kubeconfigFile string
configOverrides tcmd.ConfigOverrides
closeFn func() error
}

func NewClusterClient(kubeconfig string) (*clusterClient, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be New(...) so that people who import the package call clusterclient.New(...) instead of clusterclient.NewClusterClient(...) which is redundantly long (for go style).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I think the original author named it NewClusterClient is because it lives in the clusterdeployer package which already has a New(...) method. Happy to move it to another package in another PR if you agree that is the thing to do (I think it probably is?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving it in another PR would be great. Want to open an issue and label it as help-wanted? That would be a good thing for someone who wants to start sending code to take a crack at.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue here: #398

@@ -65,15 +67,16 @@ func (c *clusterClient) removeKubeconfigFile() error {
return os.Remove(c.kubeconfigFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does createTempFile handle removing the file automatically? I'm guessing not since we call os.Remove here. Which means that if there is an error creating the client we leak the file. We need to defer removing the file so that it gets cleaned up even on the error return paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a PR here: #390, which addresses this issue in two places. Note that there is still an opportunity to leak files if the user prematurely terminates the program or an automated system kills the VM / container. So in any sort of automated system we will need cleanup processes that handle leaked files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #390 merged and I have rebased this commit on top.

@@ -164,15 +167,31 @@ func (c *clusterClient) WaitForClusterV1alpha1Ready() error {

func (c *clusterClient) kubectlApply(manifest string) error {
r := strings.NewReader(manifest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why r is a separate variable instead of being inlined below (cmd.Stdin = strings.NewReader(manifest)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I should have changed this too in the refactor of the method. Will change.

@@ -164,15 +167,31 @@ func (c *clusterClient) WaitForClusterV1alpha1Ready() error {

func (c *clusterClient) kubectlApply(manifest string) error {
r := strings.NewReader(manifest)
cmd := exec.Command("kubectl", "apply", "--kubeconfig", c.kubeconfigFile, "-f", "-")
args := c.buildKubectlArgs("apply")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we are going to print args or do something else with it, just put the call inline below

cmd := exec.Command("kubectl", c.buildKubectlArgs("apply")...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, changed.

@@ -3,8 +3,12 @@ Usage:
clusterctl delete cluster [flags]

Flags:
--cluster string The name of the kubeconfig cluster to use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This help text isn't super clear without reading the code. Maybe "The name of the cluster to use in the kubeconfig file"?

Copy link
Contributor Author

@spew spew Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get this help text for 'free' from the libraries so it matches what kubectl has for help text (and actually the entire ecosystem of CLIs that are using the same client-go libraries). I do agree the message isn't super clear. However, there are some nice things about providing a consistent experience as users who are familiar with kubectl would know exactly what this means. So I see three options:

  1. Improve the message and diverge from what kubectl prints out, open PR upstream in client-go to improve the kubectl string.
  2. Leave message as it is (no custom code / message). Open PR to improve the messaging in client-go, wait for that to get vendored in.
  3. Run with our own message and diverge completely from the rest of the kubernetes community.

Of these options I think I like #2 the best as it is the least work and the only users right now are early adopters. It will be less work because we don't have to remember to remove our custom message later once client-go picks up the new messaging. However, I'm happy to do #1 if you think that is best. What do you think? (or do you have another idea?).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your second option seems reasonable to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will follow up with another PR in kube core -- I have an open issue here: #397

-h, --help help for cluster
--kubeconfig string Path to the kubeconfig file to use for connecting to the cluster to be deleted, if empty, the default KUBECONFIG load path is used.
-n, --namespace string If present, the namespace scope for this CLI request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is namespace for the kubectl apply commands, which isn't super clear from the help text.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for kubectl apply true, but will also be the namespace for everything clusterctl delete cluster does as the clients will be configured for this namespace.

Copy link
Contributor Author

@spew spew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed a couple of issues and left open some questions for @roberthbailey before I go implement changes.

clientSet clientset.Interface
kubeconfigFile string
configOverrides tcmd.ConfigOverrides
closeFn func() error
}

func NewClusterClient(kubeconfig string) (*clusterClient, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I think the original author named it NewClusterClient is because it lives in the clusterdeployer package which already has a New(...) method. Happy to move it to another package in another PR if you agree that is the thing to do (I think it probably is?)

@@ -65,15 +67,16 @@ func (c *clusterClient) removeKubeconfigFile() error {
return os.Remove(c.kubeconfigFile)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a PR here: #390, which addresses this issue in two places. Note that there is still an opportunity to leak files if the user prematurely terminates the program or an automated system kills the VM / container. So in any sort of automated system we will need cleanup processes that handle leaked files.

@@ -65,15 +67,16 @@ func (c *clusterClient) removeKubeconfigFile() error {
return os.Remove(c.kubeconfigFile)
}

func NewClusterClientFromFile(kubeconfigFile string) (*clusterClient, error) {
c, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfigFile)
func NewClusterClientFromDefaultSearchPath(kubeconfigFile string, overrides tcmd.ConfigOverrides) (*clusterClient, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both versions of this function treat the kubeconfigFile argument as a string. With the addition of the ability to select a specific Context in the Kubeconfig search path, I chose to rename the method to more accurately reflect reality, namely, if an empty string is passed in for kubeconfigFile then when iclientcmd.NewClusterApiClientForDefaultSearchPath(...) is called it will search for all kubeconfigs in the default search path ($KUBECONFIG, ${HOME}/.kube/config, etc).

What do you think?

@@ -164,15 +167,31 @@ func (c *clusterClient) WaitForClusterV1alpha1Ready() error {

func (c *clusterClient) kubectlApply(manifest string) error {
r := strings.NewReader(manifest)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I should have changed this too in the refactor of the method. Will change.

@@ -164,15 +167,31 @@ func (c *clusterClient) WaitForClusterV1alpha1Ready() error {

func (c *clusterClient) kubectlApply(manifest string) error {
r := strings.NewReader(manifest)
cmd := exec.Command("kubectl", "apply", "--kubeconfig", c.kubeconfigFile, "-f", "-")
args := c.buildKubectlArgs("apply")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, changed.

@@ -3,8 +3,12 @@ Usage:
clusterctl delete cluster [flags]

Flags:
--cluster string The name of the kubeconfig cluster to use
Copy link
Contributor Author

@spew spew Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get this help text for 'free' from the libraries so it matches what kubectl has for help text (and actually the entire ecosystem of CLIs that are using the same client-go libraries). I do agree the message isn't super clear. However, there are some nice things about providing a consistent experience as users who are familiar with kubectl would know exactly what this means. So I see three options:

  1. Improve the message and diverge from what kubectl prints out, open PR upstream in client-go to improve the kubectl string.
  2. Leave message as it is (no custom code / message). Open PR to improve the messaging in client-go, wait for that to get vendored in.
  3. Run with our own message and diverge completely from the rest of the kubernetes community.

Of these options I think I like #2 the best as it is the least work and the only users right now are early adopters. It will be less work because we don't have to remember to remove our custom message later once client-go picks up the new messaging. However, I'm happy to do #1 if you think that is best. What do you think? (or do you have another idea?).

-h, --help help for cluster
--kubeconfig string Path to the kubeconfig file to use for connecting to the cluster to be deleted, if empty, the default KUBECONFIG load path is used.
-n, --namespace string If present, the namespace scope for this CLI request
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for kubectl apply true, but will also be the namespace for everything clusterctl delete cluster does as the clients will be configured for this namespace.

@k4leung4
Copy link
Contributor

no additional beyond what @roberthbailey have pointed out.
lgtm

…ance delete to load provider components from cluster.
@roberthbailey
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 25, 2018
@k8s-ci-robot k8s-ci-robot merged commit 3784fb3 into kubernetes-sigs:master Jun 25, 2018
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants